﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace dogd.Tests
{
	public class NonPersistableParameters
	{
	}

	[Table("Customer")]
	public class PersistableParameters
	{
		[PrimaryKeyColumn("ID")]
		public int PrimaryKey
		{
			get;
			set;
		}

		[LookupColumn("Name")]
		public string Name
		{
			get;
			set;
		}
	}


	public class ParameterBuilderFacts
	{
		public class TheConstructForReadMethod
		{
			[Fact]
			public void CreatesSingleParameterForPrimaryKeyLookup()
			{
				var fakeCommand = new Fakes.FakeDbCommand();
				int dbKeyValue = 1000;
				var key = new Key(dbKeyValue);

				var metadata = new TableMetadata(typeof(PersistableParameters));
				var builder = new ParameterBuilder<PersistableParameters>(new Fakes.FakeParameterFactory());

				builder.ConstructForRead(key,
										metadata.PrimaryKey,
										fakeCommand);

				Assert.Equal(1, fakeCommand.Parameters.Count);
				Assert.Equal(System.Data.DbType.Int32, fakeCommand.Parameters[0].DbType);
				Assert.Equal("@PrimaryKey", fakeCommand.Parameters[0].ParameterName);
				Assert.Equal(dbKeyValue, (int)fakeCommand.Parameters[0].Value);
			}

			[Fact]
			public void CreatesSingleParameterForStringLookup()
			{
				var fakeCommand = new Fakes.FakeDbCommand();
				var id = "1234567890";

				var metadata = new TableMetadata(typeof(PersistableParameters));
				var builder = new ParameterBuilder<PersistableParameters>(new Fakes.FakeParameterFactory());

				builder.ConstructForRead(id,
										metadata.Lookup,
										fakeCommand);

				Assert.Equal(1, fakeCommand.Parameters.Count);
				Assert.Equal(System.Data.DbType.String, fakeCommand.Parameters[0].DbType);
				Assert.Equal("@Name", fakeCommand.Parameters[0].ParameterName);
				Assert.Equal(id, (string)fakeCommand.Parameters[0].Value);
			}
		}
	}
}
